package mi.payment.transaction.revoke;

import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.constants.AccountType;
import mi.payment.constants.Params;
import mi.payment.controller.BaseController;
import mi.payment.db.PayDetailModel;
import mi.payment.db.dao.PayDetail;
import mi.payment.admin.pay88.WebServiceApi;
import mi.payment.ui.base.PageRoute;
import mi.payment.ui.base.ParentActivity;

public class RevokeCodeController extends BaseController {

    public RevokeCodeController(ParentActivity activity) {
        super(activity);
    }

    public void revokeTask(PayDetail payDetail) {
        TradeData reqTradeData = initTradeData(payDetail);
        qrCodeTransRevoke(reqTradeData);
    }

    public TradeData initTradeData(PayDetail originPayDetail) {
        TradeData tradeData = Params.newTradeData();
        if (originPayDetail != null) {
            PayDetail newPayDetail = tradeData.getPayDetail();
            // 赋值金额
            newPayDetail.setAmount(originPayDetail.amount);
            // 赋值原授权号
            newPayDetail.setOriginalAuthNo(originPayDetail.authNo);
            // 赋值原交易参考号
            newPayDetail.setOriginalReferNo(originPayDetail.referNo);
            // 原交易流水号
            newPayDetail.setOriginalPOSNum(originPayDetail.voucherNo);
            // 原交易日期
            newPayDetail.setOriginalTransDate(originPayDetail.TradeDate);
            // 原交易批次号
            newPayDetail.setOriginalBathNo(originPayDetail.BathNo);
            // 扫码支付平台订单号
            newPayDetail.setThirdTransNum(originPayDetail.thirdTransNum);
            // 设置交易类型:扫码支付消费撤销
            newPayDetail.setTransactionType(2);
            newPayDetail.payQRCode = originPayDetail.payQRCode;
            newPayDetail.accountType = AccountType.OA.getValue();
            newPayDetail.transPlatform = originPayDetail.transPlatform;
            newPayDetail.qrCodeScanModel = originPayDetail.qrCodeScanModel;

            newPayDetail.referNo = originPayDetail.referNo;
            newPayDetail.customOrderNo = originPayDetail.customOrderNo;

            // L3自定义订单号赋值
            if (Params.sL3Params != null) {
                newPayDetail.customOrderNo = Params.sL3Params.transId;
            }
            newPayDetail.transType = "SV";
        }
        return tradeData;
    }

    public void qrCodeTransRevoke(TradeData tradeData) {
        // 撤销
        tradeData.getPayDetail().setTransactionType(2);
        mActivity.showLoadingDialog(R.string.load_connecting);
        mActivity.executeTask(
                () -> {
                    try {
                        TradeData resTradeData = WebServiceApi.getInstance().voidTransaction(tradeData);
                        PayDetail resPay = resTradeData.getPayDetail();
                        PayDetailModel.getInstance().insert(resPay);
                        PageRoute.startAction(mActivity, PageRoute.TYPE_QR_CODE_REVOKE, resPay);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        mActivity.dismissLoadingDialog();
                    }
                }
        );
    }


}
